package com.osce.basic.service;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csce.domain.entity.basic.BasicAssetList;
import com.douodu.excel.status.ImportStatus;
import com.osce.basic.mapper.BasicAssetListMapper;
import com.osce.common.utils.DateUtils;
import com.osce.interfaces.basic.IBasicAssetListService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 资产管理Service业务层处理
 *
 * @author zzx
 * @since 2023-09-20
 */
@Service
public class BasicAssetListServiceImpl extends ServiceImpl<BasicAssetListMapper, BasicAssetList> implements IBasicAssetListService {
    private static final Log log = LogFactory.get(IBasicAssetListService.class);

    /**
     * 查询资产管理
     *
     * @param id 资产管理主键
     * @return 资产管理
     */
    @Override
    public BasicAssetList selectBasicAssetListById(Long id) {
        return baseMapper.selectBasicAssetListById(id);
    }

    /**
     * 查询资产管理列表
     *
     * @param basicAssetList 资产管理
     * @return 资产管理
     */
    @Override
    public List<BasicAssetList> selectBasicAssetListList(BasicAssetList basicAssetList) {
        return baseMapper.selectBasicAssetListList(basicAssetList);
    }

    /**
     * 新增资产管理
     *
     * @param basicAssetList 资产管理
     * @return 结果
     */
    @Override
    public int insertBasicAssetList(BasicAssetList basicAssetList) {
        basicAssetList.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertBasicAssetList(basicAssetList);
    }

    /**
     * 修改资产管理
     *
     * @param basicAssetList 资产管理
     * @return 结果
     */
    @Override
    public int updateBasicAssetList(BasicAssetList basicAssetList) {
        return baseMapper.updateBasicAssetList(basicAssetList);
    }

    /**
     * 批量删除资产管理
     *
     * @param ids 需要删除的资产管理主键
     * @return 结果
     */
    @Override
    public int deleteBasicAssetListByIds(Long[] ids) {
        return baseMapper.deleteBasicAssetListByIds(ids);
    }

    /**
     * 删除资产管理信息
     *
     * @param id 资产管理主键
     * @return 结果
     */
    @Override
    public int deleteBasicAssetListById(Long id) {
        return baseMapper.deleteBasicAssetListById(id);
    }

    @Override
    public String importInsertBasicAssetList(List<BasicAssetList> list, int type) {
        ImportStatus importStatus = new ImportStatus();//提示语句对象
        int index = 1;
        for (BasicAssetList asset : list) {
            try {
                if (0 != type) {
                    asset.setType(type);
                }
                insertBasicAssetList(asset);
                importStatus.addSuccessMsg(index, asset.getName());
            } catch (DuplicateKeyException e) {
                importStatus.addFailMsg(index, "数据重复");
                log.error("", e);
            } catch (Exception e) {
                importStatus.addFailError(index, e);
                log.error("", e);
            } finally {
                index++;
            }
        }
        return importStatus.toHtmlString();
    }
}
